"""
Pydantic 数据模型
定义 API 请求和响应的数据结构
"""
from pydantic import BaseModel, ConfigDict, Field
from typing import Optional, List, Dict
from datetime import datetime

def to_camel(string: str) -> str:
    """将snake_case转换为camelCase"""
    components = string.split('_')
    return components[0] + ''.join(x.title() for x in components[1:])

class StockResponse(BaseModel):
    """股票基本信息响应（增强版 - 包含买卖信号）"""
    model_config = ConfigDict(
        alias_generator=to_camel,
        populate_by_name=True,
        from_attributes=True
    )
    
    symbol: str
    name: str
    current_price: float = Field(alias="currentPrice")
    change: float
    change_percent: float = Field(alias="changePercent")
    volume: int
    sector: Optional[str] = None
    market_cap: Optional[float] = Field(None, alias="marketCap")
    
    # 买卖信号信息（可选，仅推荐股票有）
    signal_type: Optional[str] = Field(None, alias="signalType")       # BUY, STRONG_BUY, SELL, STRONG_SELL, HOLD
    signal_strength: Optional[int] = Field(None, alias="signalStrength")   # 信号强度 0-100
    buy_price: Optional[float] = Field(None, alias="buyPrice")         # 建议买入价格
    target_price: Optional[float] = Field(None, alias="targetPrice")    # 目标价格
    stop_loss: Optional[float] = Field(None, alias="stopLoss")       # 止损价格
    signal_reason: Optional[str] = Field(None, alias="signalReason")     # 信号原因（App首页显示）
    signal_date: Optional[str] = Field(None, alias="signalDate")       # 信号日期

class StockHistoryResponse(BaseModel):
    """股票历史数据响应"""
    timestamp: int
    open: float
    high: float
    low: float
    close: float
    volume: int

class TradingSignalResponse(BaseModel):
    """交易信号响应"""
    symbol: str
    signal_type: str  # STRONG_BUY, BUY, HOLD, SELL, STRONG_SELL
    strength: int  # 0-100
    price: float
    action_price: Optional[float] = None  # 建议买入/卖出价格
    target_price: Optional[float] = None
    stop_loss: Optional[float] = None
    reason: str
    indicators: Dict  # JSON 对象
    action_time: Optional[str] = None

class TradingPointResponse(BaseModel):
    """买卖点响应"""
    timestamp: int
    price: float
    type: str  # BUY or SELL
    reason: str
    strength: int

class StockAnalysisResponse(BaseModel):
    """股票分析响应"""
    symbol: str
    name: str
    industry: str
    current_price: float
    trading_signal: Optional[TradingSignalResponse]
    buy_points: List[TradingPointResponse]
    sell_points: List[TradingPointResponse]
    latest_action: str
    action_time: Optional[str] = None

